#! user/bin/python
# -*- coding: utf-8 -*-

"""
Created on 2021/7/1 14:50

@File: 组件destroy与quit之间的影响.py
@Author: LT
@Description: Use of this module
"""
from tkinter import *
from tkinter.messagebox import askyesno


def onDeleteRequest():
    print("Got wm delete")
    root.destroy()


def doRootDestroy(event):
    print("Got event <Destroy>")
    if event.widget == text:
        print("for text")
        print(text.edit_modified())                             # Tcl错误：无效的部件
        ans = askyesno("Save stuff?", "Save?")                  # 可能表现得糟糕
        if ans: print(text.get("1.0", END + "-1c"))             # Tcl错误：无效的部件


root = Tk()
text = Text(root, undo=1, autoseparator=1)
text.pack()
root.bind("<Destroy>", doRootDestroy)                           # 当销毁窗口组件时，会触发<Destroy>，可以被bind方法捕获
root.protocol("WM_DELETE_WINDOW", onDeleteRequest)              # 在窗口X的按钮上，调用根对象和子对象

Button(root, text="Destroy", command=root.destroy).pack()       # 触发 <Destroy>
Button(root, text="Quit", command=root.quit).pack()             # Python致命错误会发生
mainloop()
